package ServletFilter;

import java.io.IOException;

import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;

/**
 * Servlet Filter implementation class Filter
 */
@WebFilter("/Filter")
public class CharactorFilter implements javax.servlet.Filter {
	String encoding=null;
    /**
     * Default constructor. 
     */
    public CharactorFilter() {
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see CharactorFilter#destroy()
	 */
	public void destroy() {
		encoding=null;
	}

	/**
	 * @see CharactorFilter#doFilter(ServletRequest, ServletResponse, FilterChain)
	 */
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		if(encoding!=null) {
			request.setCharacterEncoding(encoding);
			response.setContentType("text/html;charset="+encoding);
		}                                                          
		chain.doFilter(request, response);   
	}

	/**
	 * @see CharactorFilter#init(FilterConfig)
	 */
	public void init(FilterConfig fConfig) throws ServletException {
		encoding =fConfig.getInitParameter("encoding");
	}

}